package cn.iocoder.yudao.module.project.service.productioncodeorderedit;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.project.controller.admin.productioncodeorderedit.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.productioncodeorderedit.ProductionCodeOrderEditDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.project.dal.mysql.productioncodeorderedit.ProductionCodeOrderEditMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.project.enums.ErrorCodeConstants.*;

/**
 * 订单编辑-删除相关成品码 Service 实现类
 *
 * @author wangzuoyang
 */
@Service
@Validated
public class ProductionCodeOrderEditServiceImpl implements ProductionCodeOrderEditService {

    @Resource
    private ProductionCodeOrderEditMapper productionCodeOrderEditMapper;

    @Override
    public Long createProductionCodeOrderEdit(ProductionCodeOrderEditSaveReqVO createReqVO) {
        // 插入
        ProductionCodeOrderEditDO productionCodeOrderEdit = BeanUtils.toBean(createReqVO, ProductionCodeOrderEditDO.class);
        productionCodeOrderEditMapper.insert(productionCodeOrderEdit);
        // 返回
        return productionCodeOrderEdit.getRecycleId();
    }

    @Override
    public void updateProductionCodeOrderEdit(ProductionCodeOrderEditSaveReqVO updateReqVO) {
        // 校验存在
        validateProductionCodeOrderEditExists(updateReqVO.getRecycleId());
        // 更新
        ProductionCodeOrderEditDO updateObj = BeanUtils.toBean(updateReqVO, ProductionCodeOrderEditDO.class);
        productionCodeOrderEditMapper.updateById(updateObj);
    }

    @Override
    public void deleteProductionCodeOrderEdit(Long id) {
        // 校验存在
        validateProductionCodeOrderEditExists(id);
        // 删除
        productionCodeOrderEditMapper.deleteById(id);
    }

    private void validateProductionCodeOrderEditExists(Long id) {
        if (productionCodeOrderEditMapper.selectById(id) == null) {
            throw exception(PRODUCTION_CODE_ORDER_EDIT_NOT_EXISTS);
        }
    }

    @Override
    public ProductionCodeOrderEditDO getProductionCodeOrderEdit(Long id) {
        return productionCodeOrderEditMapper.selectById(id);
    }

    @Override
    public PageResult<ProductionCodeOrderEditDO> getProductionCodeOrderEditPage(ProductionCodeOrderEditPageReqVO pageReqVO) {
        return productionCodeOrderEditMapper.selectPage(pageReqVO);
    }

}